package com.ruoyi.project.manhua.webLook.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.qiniu.QiNiuYunUtils;
import com.ruoyi.framework.redis.RedisKey;
import com.ruoyi.framework.redis.RedisUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.manhua.chapter.domain.MhChapter;
import com.ruoyi.project.manhua.collect.domain.MhCollect;
import com.ruoyi.project.manhua.comment.domain.MhComment;
import com.ruoyi.project.manhua.comment.service.IMhCommentService;
import com.ruoyi.project.manhua.name.domain.MhName;
import com.ruoyi.project.manhua.name.service.IMhNameService;
import com.ruoyi.project.manhua.newXml.MhChapterNewMapper;
import com.ruoyi.project.manhua.newXml.MhCollectNewMapper;
import com.ruoyi.project.manhua.newXml.MhCommentNewMapper;
import com.ruoyi.project.manhua.newXml.MhNameNewMapper;
import com.ruoyi.project.manhua.webLook.entity.*;
import com.ruoyi.project.manhua.webLook.enumClass.MhIndexEnum;
import com.ruoyi.project.manhua.webLook.service.MhOverService;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

@Service
public class MhOverServiceImpl implements MhOverService {


    /**
     * 漫画数据
     */
    @Autowired
    private MhNameNewMapper mhNameNewMapper;

    @Autowired
    private IMhNameService mhNameService;

    /**
     * 漫画篇章
     */
    @Autowired
    private MhChapterNewMapper mhChapterNewMapper;
    /**
     * 收藏数据
     */
    @Autowired
    private MhCollectNewMapper mhCollectNewMapper;

    /**
     * 评论数据
     */
    @Autowired
    private MhCommentNewMapper mhCommentNewMapper;

    /**
     * 评论数据
     */
    @Autowired
    private IMhCommentService iMhCommentService;

    /**
     * 七牛云工具类
     */
    @Autowired
    private QiNiuYunUtils qiNiuYunUtils;

    /*启用redis开关*/
    @Value("${spring.redis.enabled}")
    private boolean enabled;


    @Autowired
    private RedisUtils redisUtils;


    /**
     * 返回查看信息数据
     *
     * @return
     */
    @Override
    public AjaxResult overData(Long id) {

        MhName mhName = mhNameService.selectMhNameById(id);
        /*推荐数据*/
        List<MhName> mhNames = null;
        /*篇章数据*/
        List<MhChapter> mhChapters = null;
        /*错误状态*/
        Integer code = Constants.IntegerStatusUtils.one;
        /*错误 提示*/
        String msg = "";

        /*=====第一步：查看有没有 此漫画数据======*/
        /*如果没有 */
        if (ObjectUtils.isEmpty(mhName)) {
            throw new BusinessException("没有此数据^_^...");
        }

        /*=====第二步：查询推荐数据======*/
        if (enabled) {
            mhNames = (List<MhName>) redisUtils.get(RedisKey.overRedis.recommend.getKey() + id);

            if (CollectionUtils.isEmpty(mhNames)) {
                mhNames = getRecommend(mhName.getName());
                /*推荐数据*/
                redisUtils.set(RedisKey.overRedis.recommend.getKey() + id, mhNames, RedisKey.time);
            }
        } else {
            mhNames = getRecommend(mhName.getName());
        }



        /*=====第三步：查询漫画篇章 数据======*/

        if (enabled) {

            mhChapters = (List<MhChapter>) redisUtils.get(RedisKey.overRedis.over_mh_chapter.getKey() + Constants.mysqlOrder.desc + "-" + Constants.IntegerStatusUtils.zero + "-" + id);

            /*redis 中有没有数据*/
            if (ObjectUtils.isEmpty(mhChapters)) {
                /*30条是漫画篇章的数据*/
                mhChapters = mhChapterNewMapper.selectWebMhChapterList(new MhWebChapter(
                        id,
                        Constants.mysqlOrder.desc,
                        Constants.IntegerStatusUtils.zero,
                        30
                ));

                /*篇章数据*/
                redisUtils.set(RedisKey.overRedis.over_mh_chapter.getKey() + Constants.mysqlOrder.desc + "-" + Constants.IntegerStatusUtils.zero + "-" + id,
                        mhChapters,
                        RedisKey.time);
            }

        } else {

            /*30条是漫画篇章的数据*/
            mhChapters = mhChapterNewMapper.selectWebMhChapterList(new MhWebChapter(
                    id,
                    Constants.mysqlOrder.desc,
                    Constants.IntegerStatusUtils.zero,
                    30
            ));

        }

        if (mhChapters.size() == Constants.IntegerStatusUtils.zero) {
            code = Constants.IntegerStatusUtils.two;
            msg = "本漫画数据丢失^_^...";
        } else if (mhChapters.size() < 30) {
            code = Constants.IntegerStatusUtils.three;
            msg = "全部篇章^_^...";
        }

        AjaxResult ajaxResult = new AjaxResult();


        /*如果 用户信息不等于 空 将用户头像返回*/
        if (!ObjectUtils.isEmpty(ShiroUtils.getSysUser())) {

            User sysUser = ShiroUtils.getSysUser();

            //mhNames.add(new MhName(ShiroUtils.getSysUser().getAvatar()));
            ajaxResult.put("avatar", ShiroUtils.getSysUser().getAvatar());

            /*是否收藏*/
            List<MhCollect> mhCollects = mhCollectNewMapper.selectWebMhCollectList(new MhWebCollect(id, sysUser.getUserId()));

            if (CollectionUtils.isEmpty(mhCollects)) {
                ajaxResult.put("collect", false);
            } else {
                ajaxResult.put("collect", true);
            }

        } else {
            ajaxResult.put("collect", false);
        }


        /*=====第七步：添加数据返回 ======*/

        /*漫画评论条数*/
        ajaxResult.put("nameCount", mhName.getComment());
        /*漫画数据*/
        ajaxResult.put("manhua", mhName);
        /*推荐*/
        ajaxResult.put("recommend", mhNames);
        /*篇章*/
        ajaxResult.put("chapters", mhChapters);
        /*判断 篇章 是否 还有数据*/
        ajaxResult.put("mhChapters", new AjaxResult(code, msg));

        ajaxResult.put("manhuaImg",Constants.getDecoder(mhName.getTheCover()));

        return ajaxResult;

    }

    /**
     * 收藏
     *
     * @param strId
     * @return
     */
    @Override
    public boolean collectService(String strId) {

        /*首先查看 用户是否登入*/
        User sysUser = ShiroUtils.getSysUser();
        if (ObjectUtils.isEmpty(sysUser)) {
            throw new BusinessException("您还没有登入，请先登入^_^...");
        }

        /*判断是不是数字*/
        boolean numeric = isNumeric(strId);
        if (!numeric) {
            throw new BusinessException("非法字符^_^...");
        }

        Long id = Long.valueOf(strId);

        /*再看看这个漫画是否存在*/
        MhName mhName = mhNameService.selectMhNameById(id);
        if (ObjectUtils.isEmpty(mhName)) {
            throw new BusinessException("此漫画不存在^_^...");
        }

        /*查看用户收藏数量*/
        Integer integer = mhCollectNewMapper.selectNameCount(sysUser.getUserId());
        if (integer >= 20) {
            throw new BusinessException("由于服务器原因漫画只能收藏20个一下^_^...");
        }

        /*r然后查看这个用户是否 收藏了这个漫画*/
        List<MhCollect> mhCollects = mhCollectNewMapper.selectWebMhCollectList(new MhWebCollect(id, sysUser.getUserId()));
        /*如果用户收藏了 漫画 那就删除此漫画*/
        if (mhCollects.size() != Constants.IntegerStatusUtils.zero) {
            /*获取第一个数据*/
            int i = mhCollectNewMapper.deleteMhCollectById(mhCollects.get(Constants.IntegerStatusUtils.zero).getId());
            if (i == Constants.IntegerStatusUtils.zero) {
                throw new BusinessException("操作失败^_^...");
            }
        } else {
            /*没有添加漫画那就 添加收藏*/
            MhCollect mhCollect = new MhCollect();
            /*漫画id*/
            mhCollect.setNameId(id);
            /*用户id*/
            mhCollect.setUserId(sysUser.getUserId());
            /*添加者*/
            mhCollect.setCreateBy(sysUser.getUserName());
            /*添加时间*/
            mhCollect.setCreateTime(new Date());
            int i = mhCollectNewMapper.insertMhCollect(mhCollect);

            /*漫画添加收藏数量*/
            int collect = mhName.getCollect();
            mhNameNewMapper.updateMhName(new MhName(++collect, mhName.getId()));
            if (i == Constants.IntegerStatusUtils.zero) {
                throw new BusinessException("操作失败^_^....");
            }
        }
        return true;
    }

    /**
     * 正序 于 倒序
     *
     * @param order
     * @return
     */
    @Override
    public List<MhChapter> orderService(String nameId, String order, Integer strip, Integer page) {
        /*判断是不是数字*/
        boolean numeric = isNumeric(nameId);
        if (!numeric) {
            throw new BusinessException("非法字符^_^...");
        }

        Long name_Id = Long.valueOf(nameId);
        if (!Constants.mysqlOrder.desc.equals(order) && !Constants.mysqlOrder.asc.equals(order)) {
            throw new BusinessException("不合法参数^_^");
        }

        List<MhChapter> mhChapters = null;

        if (enabled) {

            mhChapters = (List<MhChapter>) redisUtils.get(RedisKey.overRedis.over_mh_chapter.getKey() + order + "-" + strip + "-" + nameId);

            if (ObjectUtils.isEmpty(mhChapters)) {

                if (strip.equals(Constants.IntegerStatusUtils.zero)) {
                    /*获取前50条数据*/
                    mhChapters = mhChapterNewMapper.selectWebMhChapterList(new MhWebChapter(name_Id, order, strip, page));
                } else {
                    /*获取50条后的所有数据*/
                    mhChapters = mhChapterNewMapper.selectWebMhChapterList(new MhWebChapter(name_Id, order))
                            .stream()
                            /*跳过*/
                            .skip(page)
                            .collect(Collectors.toList());
                }
                /*篇章数据*/
                redisUtils.set(RedisKey.overRedis.over_mh_chapter.getKey() + order + "-" + strip + "-" + nameId,
                        mhChapters,
                        RedisKey.time);

            }

        } else {

            System.err.println("走数据库了。。。：nameId=" + nameId + " order=" + order + " strip=" + strip + " page=" + page);

            if (strip.equals(Constants.IntegerStatusUtils.zero)) {
                /*获取前50条数据*/
                mhChapters = mhChapterNewMapper.selectWebMhChapterList(new MhWebChapter(name_Id, order, strip, page));
            } else {
                /*获取50条后的所有数据*/
                mhChapters = mhChapterNewMapper.selectWebMhChapterList(new MhWebChapter(name_Id, order))
                        .stream()
                        /*跳过*/
                        .skip(page)
                        .collect(Collectors.toList());
            }
        }

        return mhChapters;
    }

    /**
     * 漫画评论 读取数据库
     *
     * @param nameId
     * @return
     */
    @Override
    public synchronized List<MhCommentData> commentService(String nameId, Integer strip, Integer page) {
        /*不是数字*/
        if (!isNumeric(nameId)) {
            throw new BusinessException("非法字符^_^...");
        }

        Long name_id = Long.valueOf(nameId);
        Integer strip_ip = (strip * page);
        /*排序*/
        List<MhComment> mhComments = mhCommentNewMapper.selectWebMhCommentList(new MhWebComment(
                name_id,
                Constants.LongStatusUtils.zero,
                strip_ip,
                page,
                "give"
        ));

        List<MhCommentData> mhCommentData = new ArrayList<>();
        /*数据不等于空*/
        if (!CollectionUtils.isEmpty(mhComments)) {

            mhComments.forEach((e) -> {

                MhCommentData data = new MhCommentData();

                data.setGive(e.getGive());
                data.setHead(e.getHead());
                data.setId(e.getId());
                data.setNameId(e.getNameId());
                data.setSuperior(e.getSuperior());
                data.setUserName(e.getUserName());
                data.setRemark(e.getRemark());
                data.setCreateTime(e.getCreateTime());

                MhWebComment mhWebComment = new MhWebComment();
                mhWebComment.setSuperior(e.getId());

                /*查询评论下的二级评论*/
                List<MhComment> mhWebCommentList = mhCommentNewMapper.selectWebMhCommentList(mhWebComment);

                /*如果不等于空*/
                if (!CollectionUtils.isEmpty(mhWebCommentList)) {
                    List<MhComment> mhWebCommentListdata = new ArrayList<>();
                    /*循环遍历*/
                    mhWebCommentList.forEach((i) -> {
                        MhWebComment mhWebCommenti = new MhWebComment();
                        mhWebCommenti.setSuperior(i.getId());
                        /*查询二级评论下的三级评论*/
                        List<MhComment> mhWebCommentList3 = mhCommentNewMapper.selectWebMhCommentList(mhWebCommenti);
                        if (!CollectionUtils.isEmpty(mhWebCommentList3)) {
                            mhWebCommentList3.forEach((j) -> {
                                j.setRemark(j.getRemark() + "//@" + i.getUserName() + ":" + i.getRemark());
                                mhWebCommentListdata.add(j);
                            });
                        }
                        mhWebCommentListdata.add(i);
                    });
                    data.setMhComment(mhWebCommentListdata);
                }
                mhCommentData.add(data);
            });

        }

        return mhCommentData;
    }

    /**
     * 漫画评论 读取redis
     *
     * @param nameId
     * @param strip
     * @param page
     * @return
     */
    @Override
    public List<MhCommentData> commentServiceRedis(String nameId, Integer strip, Integer page) {
        /*获取redis评论数据*/
        List<MhComment> mhComments = this.redisOneLimit(nameId, strip, page);
        /*评论盒子*/
        List<MhCommentData> mhCommentData = new ArrayList<>();

        mhComments.forEach((e) -> {
            /*一级评论*/
            MhCommentData data = new MhCommentData();
            /*对象复制*/
            BeanUtils.copyProperties(e, data);
            /*获取第一级评论下面的二级评论*/
            List<MhComment> mhComments_Superior = this.redisTwoList(nameId, e.getId());
            /*不等于null*/
            if (!CollectionUtils.isEmpty(mhComments_Superior)) {
                List<MhComment> three = this.getThree(mhComments_Superior)
                        .stream()
                        .sorted(Comparator.comparing(MhComment::getGive).reversed())
                        .collect(Collectors.toList());
                data.setMhComment(three);
            }
            mhCommentData.add(data);
        });
        return mhCommentData;
    }

    /**
     * 获取第二级与第三级评论
     *
     * @param list
     * @return
     */
    public List<MhComment> getThree(List<MhComment> list) {
        /*保存评论盒子*/
        List<MhComment> mhWebCommentList = new ArrayList<>();
        /*遍历第二级评论*/
        list.forEach((e) -> {
            /*存放评论*/
            LinkedList<MhComment> listCommentsThree = new LinkedList<>();
            /*将原始数据添加进*/
            listCommentsThree.addAll(list);
            mhWebCommentList.addAll(list);
            while (listCommentsThree.size() != 0) {
                /*删除并返回集合中第一个元素*/
                MhComment pop = listCommentsThree.pop();
                /*查找恢复评论的数据*/
                List<MhComment> mhComments = redisThreeList(pop.getNameId().toString(), pop.getId().toString());
                /*不等于null*/
                if (!CollectionUtils.isEmpty(mhComments)) {
                    /*查询回复下面是否还有回复*/
                    mhComments.forEach((r) -> {
                        String remark = r.getRemark();
                        r.setRemark(remark + "//@" + pop.getUserName() + ":" + pop.getRemark());
                        mhWebCommentList.add(r);
                        listCommentsThree.add(r);
                    });
                }
            }
        });
        /*返回数据*/
        return mhWebCommentList;
    }


    /**
     * 从redis中获取漫画第一级的数据 分页
     *
     * @param nameId 漫画的id
     * @return
     */
    @Override
    public List<MhComment> redisOneLimit(String nameId, Integer strip, Integer page) {

        Integer strip_ip = (strip * page);

        /*获取所有的值*/
        List<Object> objects = redisUtils.lGet(RedisKey.overRedis.Comment_list_manhua_id.getKey() + nameId, strip_ip, page);

        /*返回的值*/
        List<MhComment> mhComments = null;

        /*如果redis中没有数据*/

        if (CollectionUtils.isEmpty(objects)) {

            /*创建lock锁。*/
            Lock lock = new ReentrantLock();
            //获得锁
            lock.lock();

            objects = redisUtils.lGet(RedisKey.overRedis.Comment_list_manhua_id.getKey() + nameId, strip_ip, page);

            /*还等于 null 去数据库中获取数据*/
            if (CollectionUtils.isEmpty(objects)) {

                /*数据库中获取数据*/
                /*排序*/
                List<MhComment> mhCommentsMysql = mhCommentNewMapper.selectWebMhCommentList(new MhWebComment(
                        Long.valueOf(nameId),
                        Constants.StrStatusUtils.one,
                        "give"
                ));
                if (!CollectionUtils.isEmpty(mhCommentsMysql)) {
                    mhComments = mhCommentsMysql.stream()
                            .skip(strip_ip)
                            .limit(page)
                            .collect(Collectors.toList());
                    mhCommentsMysql.forEach((e) -> {
                        /*存放入redis数据库中*/
                        redisUtils.lSet(RedisKey.overRedis.Comment_list_manhua_id.getKey() + nameId, e, RedisKey.time);
                    });
                }
                /*释放锁*/
                lock.unlock();
            }

        } else {
            mhComments = new ArrayList<>();
            for (int i = 0; i < objects.size(); i++) {
                MhComment mhComment = (MhComment) objects.get(i);
                mhComments.add(mhComment);
            }

        }

        return mhComments;
    }


    /**
     * 从redis中获取漫画第二级的评论全部数据 不分页
     *
     * @param nameId
     * @return
     */
    @Override
    public List<MhComment> redisTwoList(String nameId, Long superior) {

        List<MhComment> list = null;

        List<Object> objects = redisUtils.lGet(RedisKey.overRedis.Comment_list_manhua_superior_id.getKey() + nameId + "-" + superior, 0, -1);

        if (ObjectUtils.isEmpty(objects)) {
            /*创建锁*/
            Lock lock = new ReentrantLock();
            lock.lock();
            objects = redisUtils.lGet(RedisKey.overRedis.Comment_list_manhua_superior_id.getKey() + nameId + "-" + superior, 0, -1);
            /*去数据库中获取*/
            if (ObjectUtils.isEmpty(objects)) {

                list = mhCommentNewMapper.selectWebMhCommentList(new MhWebComment(
                        Long.valueOf(nameId),
                        superior,
                        Constants.StrStatusUtils.two,
                        "give"
                ));
                if (!CollectionUtils.isEmpty(list)){
                    list.forEach((e) -> {
                        /*存放入redis数据库中*/
                        redisUtils.lSet(RedisKey.overRedis.Comment_list_manhua_superior_id.getKey() + nameId + "-" + superior, e, RedisKey.time);
                    });
                }
                /*释放锁*/
                lock.unlock();
            }
        } else {
            /*objects 第一次获取到数据，将他转换*/
            list = new ArrayList<>();

            for (int i = 0; i < objects.size(); i++) {
                MhComment mhComment = (MhComment) objects.get(i);
                list.add(mhComment);
            }

        }

        return list;
    }

    /**
     * 从redis中获取漫画第三级的评论全部数据
     *
     * @param nameId 漫画id
     * @param Id
     * @return
     */
    @Override
    public List<MhComment> redisThreeList(String nameId, String Id) {
        List<MhComment> Comment = null;
        /*判断*/
        if (redisUtils.hasKey(RedisKey.overRedis.Comment_map_manhua_id.getKey() + nameId)) {
            /*获取*/
            Comment = (List<MhComment>) redisUtils.hget(RedisKey.overRedis.Comment_map_manhua_id.getKey() + nameId, Id);
            /*不等于null返回数据*/
            if (!CollectionUtils.isEmpty(Comment)) {
                return Comment;
            }
        } else {
            /*如果key不存在*/
            List<MhComment> mhComments = mhCommentNewMapper.selectWebMhCommentList(new MhWebComment(
                    Long.valueOf(nameId),
                    Constants.StrStatusUtils.three,
                    "give"
            ));
            if (!ObjectUtils.isEmpty(mhComments)) {
                Map<Long, List<MhComment>> collect = mhComments.stream().collect(Collectors.groupingBy((e) -> e.getSuperior()));
                Comment = collect.get(Long.valueOf(Id));
                collect.forEach((key, value) -> {
                    redisUtils.hset(RedisKey.overRedis.Comment_map_manhua_id.getKey() + nameId, key.toString(), value, RedisKey.time);
                });
            }
        }
        return Comment;
    }


    /**
     * 添加 评论
     *
     * @param nameId
     * @param text
     * @return
     */
    @Override
    public MhComment commentAddService(String nameId, String text, Long id, String type) {

        /*用户是否登入*/
        if (ObjectUtils.isEmpty(ShiroUtils.getSysUser())) {
            throw new BusinessException("您还没有登入请先登入^_^..");
        }

        /*不是数字*/
        if (!isNumeric(nameId)) {
            throw new BusinessException("非法字符^_^...");
        }
        Date date = new Date();
        MhComment mhComment = new MhComment();

        /*漫画id*/
        mhComment.setNameId(Long.valueOf(nameId));
        /*评论信息*/
        mhComment.setRemark(text);
        /*点赞初始值*/
        mhComment.setGive(Constants.IntegerStatusUtils.zero);
        /*评论时间*/
        mhComment.setCreateTime(date);
        /*评论者名称*/
        mhComment.setUserName(ShiroUtils.getSysUser().getUserName());
        /*头像 */
        //ShiroUtils.getSysUser().getAvatar();
        mhComment.setHead(ShiroUtils.getSysUser().getAvatar());
        /*一级评论*/
        mhComment.setSuperior(id);
        /*评论类型*/
        mhComment.setType(type);
        /*评论者id*/
        mhComment.setUserId(ShiroUtils.getSysUser().getUserId());

        MhName mhNames = mhNameService.selectMhNameById(Long.valueOf(nameId));

        MhName mhName = new MhName();

        mhName.setId(mhNames.getId());

        Integer comment = mhNames.getComment();

        mhName.setComment(++comment);

        if (enabled) {
            /*添加*/
            this.commentInsetRedis(mhComment, "2");
        }
        /*添加*/
        mhCommentNewMapper.insertMhComment(mhComment);
        mhNameService.updateMhName(mhName);
        List<MhComment> mhComments = mhCommentNewMapper.selectMhCommentList(mhComment);
        return mhComments.get(0);
    }

    /**
     * 点赞
     *
     * @param id     点赞数据id
     * @param numder 原始点赞数
     * @return
     */
    @Override
    public Integer giveService(Long id, Integer numder) {

        if (numder > 100000) {
            throw new BusinessException("不可大于100000^_^...");
        }

        /*评论数据*/
        MhComment mhComment = iMhCommentService.selectMhCommentById(id);
        Integer give = mhComment.getGive();
        mhComment.setGive(++give);
        /*修改数据库的数据*/
        int unmder = mhCommentNewMapper.updateMhComment(new MhComment(id, give));
        redisUtils.del(RedisKey.MhCommentRedis.name_str_id.getKey() + id);
        /*是否开启redis*/
        if (enabled) {
            /*修改*/
            this.commentInsetRedis(mhComment, "1");
        }
        return unmder;
    }

    /**
     * 添加 或 修改
     *
     * @param mhComment
     * @param str       1修改 2添加
     */
    public void commentInsetRedis(MhComment mhComment, String str) {
        /*一级评论点赞 或 二级评论*/
        if ("1".equals(str)) {
            /*修改*/
            if (Constants.StrStatusUtils.one.equals(mhComment.getType()) || Constants.StrStatusUtils.two.equals(mhComment.getType())) {
                List<MhComment> mhComments = null;
                /*获取一级评论全部redis数据*/
                if (Constants.StrStatusUtils.one.equals(mhComment.getType())) {
                    mhComments = this.redisOneLimit(mhComment.getNameId().toString(), 0, -1);
                } else {
                    /*二级*/
                    mhComments = redisTwoList(mhComment.getNameId().toString(), mhComment.getSuperior());
                }
                for (int i = 0; i < mhComments.size(); i++) {
                    /*查找 漫画评论在list中的位置*/
                    if (mhComments.get(i).getId().equals(mhComment.getId())) {
                        /*修改或添加数据*/
                        if (Constants.StrStatusUtils.one.equals(mhComment.getType())) {
                            /*一级*/
                            /*修改*/
                            redisUtils.lUpdateIndex(RedisKey.overRedis.Comment_list_manhua_id.getKey() + mhComment.getNameId(), i, mhComment);

                        } else {
                            /*修改*/
                            redisUtils.lUpdateIndex(RedisKey.overRedis.Comment_list_manhua_superior_id.getKey() + mhComment.getNameId() + "-" + mhComment.getSuperior(), i, mhComment);

                        }
                        break;
                    }
                }
            } else if (Constants.StrStatusUtils.three.equals(mhComment.getType())) {
                /*三级评论*/
                List<MhComment> mhComments = redisThreeList(mhComment.getNameId().toString(), mhComment.getSuperior().toString());
                for (int i = 0; i < mhComments.size(); i++) {
                    if (mhComments.get(i).getId().equals(mhComment.getId())) {
                        mhComments.get(i).setGive(mhComment.getGive());
                    }
                }
                /*首先删除原先的值*/
                //redisUtils.hdel(RedisKey.overRedis.Comment_map_manhua_id + mhComment.getNameId().toString(), mhComment.getSuperior().toString());
                /*更新三级评论*/
                redisUtils.hset(RedisKey.overRedis.Comment_map_manhua_id.getKey() + mhComment.getNameId().toString(), mhComment.getSuperior().toString(), mhComments);
            }
        } else {
            if (Constants.StrStatusUtils.one.equals(mhComment.getType()) || Constants.StrStatusUtils.two.equals(mhComment.getType())) {
                /*添加*/
                if (Constants.StrStatusUtils.one.equals(mhComment.getType())) {
                    /*添加*/
                    redisUtils.lSet(RedisKey.overRedis.Comment_list_manhua_id.getKey() + mhComment.getNameId(), mhComment, RedisKey.time);

                } else {
                    /*添加*/
                    redisUtils.lSet(RedisKey.overRedis.Comment_list_manhua_superior_id.getKey() + mhComment.getNameId() + "-" + mhComment.getSuperior(), mhComment, RedisKey.time);
                }
            } else if (Constants.StrStatusUtils.three.equals(mhComment.getType())) {
                /*三级评论*/
                List<MhComment> mhComments = redisThreeList(mhComment.getNameId().toString(), mhComment.getSuperior().toString());
                mhComments.add(mhComment);
                /*首先删除原先的值*/
                //redisUtils.hdel(RedisKey.overRedis.Comment_map_manhua_id + mhComment.getNameId().toString(), mhComment.getSuperior().toString());
                /*更新三级评论*/
                redisUtils.hset(RedisKey.overRedis.Comment_map_manhua_id.getKey() + mhComment.getNameId().toString(), mhComment.getSuperior().toString(), mhComments);
            }
        }

    }

    /**
     * 阅读
     *
     * @param id
     * @return
     */
    @Override
    public synchronized int readingService(Long id) {
        /*获取 漫画数据*/
        MhName mhName = mhNameService.selectMhNameById(id);
        /*获取 阅读数量*/
        Integer reading = mhName.getReading();
        /*删除*/
        redisUtils.del(RedisKey.mhName.name_id.getKey() + id);
        /*修改*/
        return mhNameNewMapper.updateMhName(new MhName(id, ++reading));
    }

    /*判读是否是数字*/
    public static boolean isNumeric(final String str) {
        // null or empty
        if (str == null || str.length() == 0) {
            return false;
        }
        return str.chars().allMatch(Character::isDigit);
    }

    /*推荐数据*/
    public List<MhName> getRecommend(String name) {

        List<MhName> mhNames = null;

        /*获取 原漫画名称的长度*/
        Integer nameLenght = name.length();
        /*大于三 截取*/
        if (nameLenght > 3) {
            name = name.substring(0, (name.length() - 3));
        }

        /*查找相关的漫画*/
        mhNames = mhNameNewMapper.seletWebLimitList(new MhWebName(
                name,
                Constants.IntegerStatusUtils.zero,
                12
        ));

        /*如果小于 12 因为 推荐数据要12条*/
        if (mhNames.size() < 12) {
            /*查询最门的数据*/
            List<MhName> mhNames_reading = mhNameNewMapper.seletWebLimitList(new MhWebName(Constants.IntegerStatusUtils.zero, 12, MhIndexEnum.STATE.CREATIVE.getValue()));

            /*循环数据*/
            for (int i = 0, len = mhNames_reading.size(); i < len; i++) {
                /*如果添加够12调数据 跳出循环*/
                if (mhNames.size() >= 12) {
                    break;
                }
                mhNames.add(mhNames_reading.get(i));
            }
        }

        /*推荐数据 编码*/
        mhNames.forEach((e) -> {
            e.setTheCover(Constants.getEncoder(qiNiuYunUtils.getUrlImg(e.getTheCover())));
        });

        return mhNames;
    }

}
